function lookup(inputString, form) {
	if(inputString.length <= 2) {
		$("#" + form + " .suggestions").hide();
	} else {
		var params = setParameters(form);
		$.post("forms.php",
				params, 
				function(data){
					if(data != null && data != "") {
						$("#" + form + " .autoSuggestionsList ul").html("");
						$("#" + form + " .suggestions").show();
						switch (form) {
							case "countries": suggestCountries(data["pays"]);break;
							case "cities": suggestCities(data["ville"]);break;
							case "meetings": suggestMeetings(data["meeting"]);break;
							default: break;
						}
					}
				}, "json");
	}
}

function setParameters(form) {
	switch (form) {
		case 'countries': return {year: $("#filter_year").attr("value"),
									type: ""+form+""};
		case 'cities': return {year: $("#filter_year").attr("value"),
								country: $("#countries .inputString").val(),
								type: ""+form+""};
		case 'meetings': return {year: $("#filter_year").attr("value"),
							country: $("#countries .inputString").val(),
							city: $("#cities .inputString").val(),
							type: ""+form+""};
		default : break;
	}
}

function fill(thisValue, form) {
	$("#" + form + " .inputString").val(thisValue);
	setTimeout("$('#" + form + " .suggestions').hide();", 200);
}

function suggestCountries(data) {
	if (typeof(data) == typeof({})) {
		for (var z=0; (z<data.length && z<=20); z++) {
			$('#countries .autoSuggestionsList ul').append("<li onClick=\"fill('"
					+data[z]+"', 'countries');\">" + data[z] + "</li>");
		}
	} else {
		$('#countries .autoSuggestionsList ul').append("<li onClick=\"fill('"
				+data+"', 'countries');\">" + data + "</li>");
	}
}
function suggestCities(data) {
	if (typeof(data) == typeof({})) {
		for (var z=0; (z<data.length && z<=20); z++) {
			$('#cities .autoSuggestionsList ul').append("<li onClick=\"fill('"
					+data[z]+"', 'cities');\">" + data[z] + "</li>");
		}
	} else {
		$('#cities .autoSuggestionsList ul').append("<li onClick=\"fill('"
				+data+"', 'cities');\">" + data + "</li>");
	}
}
function suggestMeetings(data) {
	meetings_tmp = new Array();
	if (data.length > 0) {
		for (var z=0; (z<data.length && z<=20); z++) {
			meetings_tmp.push(data[z]);
			$('#meetings .autoSuggestionsList ul').append("<li onClick=\"selectMeeting('"
					+(meetings_tmp.length-1)+"');\">" + data[z]["name"] + "</li>");
		}
	} else {
		meetings_tmp.push(data);
		$('#meetings .autoSuggestionsList ul').append("<li onClick=\"selectMeeting('"
				+(meetings_tmp.length-1)+"');\">" + data["name"] + "</li>");
	}
}

function selectMeeting(meeting) {
	fill(meetings_tmp[meeting]["name"], 'meetings');
	meetings.push(meetings_tmp[meeting]);
	geocodeAndSave(meeting);
	draw_chart_themes(meetings_tmp[meeting]["themes"]["theme"], meetings_tmp[meeting]["name"]);
}

function selectCountryOrCity(thisValue, form) {
	fill(thisValue, form);
}
